package com.shbaiche.bifenba.adapter.match;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hyphenate.chat.EMMessage;
import com.shbaiche.bifenba.R;
import com.shbaiche.bifenba.model.base.TransMessage;
import com.shbaiche.bifenba.model.match.EaseChatMessage;
import com.shbaiche.bifenba.model.match.EasePhotoMessage;
import com.shbaiche.bifenba.model.match.MatchesVo;
import com.shbaiche.bifenba.net.JsonCallback;
import com.shbaiche.bifenba.net.NetApi;
import com.shbaiche.bifenba.ui.match.MatchDetailActivity;
import com.shbaiche.bifenba.util.StringUtil;
import com.shbaiche.bifenba.util.ToastUtil;
import com.shbaiche.bifenba.view.CircularImage;

import java.util.Date;
import java.util.HashMap;

import butterknife.BindView;
import cn.droidlover.xdroid.base.SimpleRecAdapter;
import cn.droidlover.xdroid.kit.KnifeKit;
import cn.droidlover.xdroidbase.imageloader.ILFactory;
import cn.droidlover.xdroidbase.kit.Kits;
import okhttp3.Call;

/**
 * 聊天室消息
 */

public class MatchBBSMessageListAdapter extends SimpleRecAdapter<EMMessage, MatchBBSMessageListAdapter.ViewHolder> {
    private Activity context;

    private String userName;

    private static HashMap<String,EasePhotoMessage> mapInfos = new HashMap<>();

    public MatchBBSMessageListAdapter(Activity context) {
        super(context);
        this.context = context;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    @Override
    public ViewHolder newViewHolder(View itemView) {
        return new ViewHolder(itemView);
    }

    @Override
    public int getLayoutId() {
        return R.layout.adapter_match_bbs_message_list;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        EMMessage item = data.get(position);

        if(item.getFrom().equals(userName)){
            holder.layoutLeft.setVisibility(View.GONE);
            holder.layoutRight.setVisibility(View.VISIBLE);
            holder.tvMessageRight.setText(getContent(item.getBody().toString()));
            holder.tvNameRight.setText("...");
            holder.tvTimeRight.setText(StringUtil.getTimeFormatText(new Date(item.getMsgTime())));
            if(mapInfos.containsKey(item.getFrom())){
                EasePhotoMessage message = mapInfos.get(item.getFrom());
                holder.tvNameRight.setText(message.getNickName());
                ILFactory.getLoader().loadNet(holder.ivLogoRight, message.getPhoto(), null);
            }else{
                loadUserInfo(item.getFrom());
            }
        }else{
            holder.layoutLeft.setVisibility(View.VISIBLE);
            holder.layoutRight.setVisibility(View.GONE);
            holder.tvMessage.setText(getContent(item.getBody().toString()));
            holder.tvName.setText("...");
            holder.tvTime.setText(StringUtil.getTimeFormatText(new Date(item.getMsgTime())));
            if(mapInfos.containsKey(item.getFrom())){
                EasePhotoMessage message = mapInfos.get(item.getFrom());
                holder.tvName.setText(message.getNickName());
                ILFactory.getLoader().loadNet(holder.ivLogo, message.getPhoto(), null);
            }else{
                loadUserInfo(item.getFrom());
            }
        }
        //



    }

    private String getContent(String str){
        if(str.startsWith("txt:")){
            try{
                str = str.replaceAll("txt:","");
                str = str.substring(1,str.length()-1);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return str;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_logo_home)
        CircularImage ivLogo;

        @BindView(R.id.tv_name_home)
        TextView tvName;

        @BindView(R.id.tv_message)
        TextView tvMessage;

        @BindView(R.id.tv_time)
        TextView tvTime;

        @BindView(R.id.iv_logo_right)
        CircularImage ivLogoRight;

        @BindView(R.id.tv_name_right)
        TextView tvNameRight;

        @BindView(R.id.tv_message_right)
        TextView tvMessageRight;

        @BindView(R.id.tv_time_right)
        TextView tvTimeRight;

        @BindView(R.id.layout_left)
        LinearLayout layoutLeft;

        @BindView(R.id.layout_right)
        LinearLayout layoutRight;



        public ViewHolder(View itemView) {
            super(itemView);
            KnifeKit.bind(this, itemView);
        }
    }

    private void loadUserInfo(final String username){
        EasePhotoMessage message = new EasePhotoMessage();
        message.setUserName(username);

        NetApi.easePhoto(message, new JsonCallback<TransMessage>() {
            @Override
            public void onFail(Call call, Exception e, int id) {
                ToastUtil.show("onFail" + e.getMessage());
            }

            @Override
            public void onError(Call call, Exception e, int id) {
                ToastUtil.show("onFail" + e.getMessage());

            }

            @Override
            public void onResponse(TransMessage response, int id) {
                EasePhotoMessage result =new Gson().fromJson(response.getMessage(),EasePhotoMessage.class);
                if (StringUtil.equalsIgnoreCase(TransMessage.SuccessCode, result.getCode())) {
                    if(result!=null && result.getNickName()!=null){
                        Log.e("t","获取头像: "+result.getPhoto());
                        mapInfos.put(username,result);

                        notifyDataSetChanged();
                    }
                }else{
                    ToastUtil.show(response.getMessage());
                }
            }
        });
    }
}
